package view.interfaces;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import business.dao.PedidoDAO;
import business.model.PedidosAndamento;
import business.model.PizzasTO;
import business.model.SaboresTO;
import business.model.tableModel.AndamentoTableModel;
import business.model.tableModel.PizzaTableModel;
import business.model.tableModel.SaboresTableModel;
import exception.BusinessException;
import exception.InterfaceException;
import exception.UsuarioException;

public class telaPizzaiolo extends JFrame {

	private JPanel contentPane;
	private JTable tblPedidos;
	private JTable tblPizzas;
	private JTable tblSabores;

	private PedidoDAO pedidoDAO = new PedidoDAO();
	private AndamentoTableModel andamentoTableModel;
	private PedidosAndamento pedidosAndamento = new PedidosAndamento();
	private List<PizzasTO> pizzasTO = new ArrayList<PizzasTO>();
	private PizzaTableModel pizzaTableModel;
	private SaboresTO saboresTO = new SaboresTO();
	private SaboresTableModel saboresTableModel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					telaPizzaiolo frame = new telaPizzaiolo();
					frame.setExtendedState(frame.getExtendedState()
							| JFrame.MAXIMIZED_BOTH);
					frame.setVisible(true);
					frame.listarAndamento();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void listarAndamento() {
		// TODO Auto-generated method stub

		try {
			List<PedidosAndamento> andamentos = pedidoDAO
					.listarPedidosAndamento();
			andamentoTableModel = new AndamentoTableModel(andamentos);
			tblPedidos.setModel(andamentoTableModel);

		} catch (UsuarioException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterfaceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame.
	 */
	public telaPizzaiolo() {
		setTitle("Pizzaiolo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 720, 489);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JTextPane txtpnLista = new JTextPane();
		txtpnLista.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtpnLista.setBackground(SystemColor.control);
		txtpnLista.setText("Pedidos:");

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));

		JButton btnVoltar = new JButton("VOLTAR");
		btnVoltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new telaPrincipal().setVisible(true);
				setVisible(false);
			}
		});

		btnVoltar.setFont(new Font("Tahoma", Font.BOLD, 12));

		JLabel lblOlPizzaiolo = new JLabel("Ol\u00E1 Pizzaiolo!");
		lblOlPizzaiolo
				.setIcon(new ImageIcon(
						"C:\\Users\\Michael\\Documents\\Faculdade\\Plano de Ensino - Disciplina Projeto Interativo IV\\Projeto Pizzaria\\Pizzaria\\src\\tumblr_lln52xMpNv1qab04f.gif"));
		lblOlPizzaiolo.setForeground(Color.RED);
		lblOlPizzaiolo.setFont(new Font("Traditional Arabic", Font.BOLD, 20));

		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				panel,
																				GroupLayout.DEFAULT_SIZE,
																				390,
																				Short.MAX_VALUE)
																		.addGap(18)
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addGroup(
																								gl_contentPane
																										.createSequentialGroup()
																										.addComponent(
																												txtpnLista,
																												GroupLayout.DEFAULT_SIZE,
																												112,
																												Short.MAX_VALUE)
																										.addGap(164))
																						.addComponent(
																								scrollPane,
																								GroupLayout.DEFAULT_SIZE,
																								276,
																								Short.MAX_VALUE)))
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				lblOlPizzaiolo,
																				GroupLayout.PREFERRED_SIZE,
																				246,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				305,
																				Short.MAX_VALUE)
																		.addComponent(
																				btnVoltar,
																				GroupLayout.PREFERRED_SIZE,
																				133,
																				GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																lblOlPizzaiolo,
																GroupLayout.PREFERRED_SIZE,
																70,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				btnVoltar)))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																panel,
																Alignment.TRAILING,
																GroupLayout.DEFAULT_SIZE,
																334,
																Short.MAX_VALUE)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				txtpnLista,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				scrollPane,
																				GroupLayout.DEFAULT_SIZE,
																				308,
																				Short.MAX_VALUE)))));

		tblPedidos = new JTable();
		tblPedidos.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (arg0.getClickCount() == 2) {
					PedidosAndamento andamento = andamentoTableModel.andamento
							.get(tblPedidos.getSelectedRow());
					tblPizzas.setModel(new PizzaTableModel(
							new ArrayList<PizzasTO>()));
					tblSabores.setModel(new SaboresTableModel(
							new ArrayList<SaboresTO>()));
					try {
						pizzasTO = pedidoDAO.listarPizzasPorPedidos(andamento);
						pedidosAndamento = andamento;

						pizzaTableModel = new PizzaTableModel(pizzasTO);
						tblPizzas.setModel(pizzaTableModel);

					} catch (UsuarioException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (BusinessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterfaceException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}
		});
		scrollPane.setViewportView(tblPedidos);

		JTextPane txtpnObservaes = new JTextPane();
		txtpnObservaes.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtpnObservaes.setBackground(SystemColor.control);
		txtpnObservaes.setEditable(false);
		txtpnObservaes.setText("Observa\u00E7\u00F5es:");

		JTextPane textPane_3 = new JTextPane();
		textPane_3.setBorder(new LineBorder(new Color(0, 0, 0)));
		textPane_3.setEditable(false);

		JButton btnFinalizar = new JButton("Finalizar");
		btnFinalizar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pedidoDAO.finalizarPedido(pedidosAndamento);
				tblPizzas.setModel(new PizzaTableModel(
						new ArrayList<PizzasTO>()));
				tblSabores.setModel(new SaboresTableModel(
						new ArrayList<SaboresTO>()));
				listarAndamento();
			}
		});
		btnFinalizar.setFont(new Font("Tahoma", Font.BOLD, 12));

		JLabel lblPedidoEmAndamento = new JLabel("Pedido em Andamento");
		lblPedidoEmAndamento.setFont(new Font("Tahoma", Font.BOLD, 12));

		JScrollPane scrollPane_1 = new JScrollPane();

		JScrollPane scrollPane_2 = new JScrollPane();

		JTextPane txtpnPizzas = new JTextPane();
		txtpnPizzas.setText("Pizzas");
		txtpnPizzas.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtpnPizzas.setEditable(false);
		txtpnPizzas.setBackground(SystemColor.menu);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGap(126)
								.addComponent(btnFinalizar,
										GroupLayout.DEFAULT_SIZE, 137,
										Short.MAX_VALUE).addGap(125))
				.addGroup(
						gl_panel.createSequentialGroup().addContainerGap()
								.addComponent(lblPedidoEmAndamento)
								.addContainerGap(238, Short.MAX_VALUE))
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addComponent(textPane_3,
										GroupLayout.DEFAULT_SIZE, 368,
										Short.MAX_VALUE).addContainerGap())
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addComponent(scrollPane_1,
										GroupLayout.DEFAULT_SIZE, 144,
										Short.MAX_VALUE)
								.addGap(10)
								.addComponent(scrollPane_2,
										GroupLayout.DEFAULT_SIZE, 214,
										Short.MAX_VALUE).addContainerGap())
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addComponent(txtpnObservaes,
										GroupLayout.DEFAULT_SIZE, 214,
										Short.MAX_VALUE).addGap(164))
				.addGroup(
						Alignment.LEADING,
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addComponent(txtpnPizzas,
										GroupLayout.PREFERRED_SIZE, 155,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap(223, Short.MAX_VALUE)));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGap(7)
								.addComponent(lblPedidoEmAndamento)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(txtpnPizzas,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(
														scrollPane_1,
														GroupLayout.DEFAULT_SIZE,
														108, Short.MAX_VALUE)
												.addComponent(
														scrollPane_2,
														GroupLayout.DEFAULT_SIZE,
														108, Short.MAX_VALUE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(txtpnObservaes,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(textPane_3,
										GroupLayout.DEFAULT_SIZE, 46,
										Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnFinalizar).addContainerGap()));

		tblSabores = new JTable();
		scrollPane_2.setViewportView(tblSabores);

		tblPizzas = new JTable();
		tblPizzas.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int indice = tblPizzas.getSelectedRow();

					saboresTableModel = new SaboresTableModel(pizzasTO.get(
							indice).getSaboresTO());
					tblSabores.setModel(saboresTableModel);

				}
			}
		});
		scrollPane_1.setViewportView(tblPizzas);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}
}
